package com.yueya.api.donate;

import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.yueya.business.donate.Donate;
import com.yueya.common.constant.CodeConst;
import com.yueya.common.model.Resp;
import com.yueya.common.service.BaseService;
import com.yueya.common.util.DateUtil;
import com.yueya.common.util.UUIDGenerator;

import net.sf.json.JSONObject;

public class IDonateService extends BaseService {

	//根据知识分类查询知识
	public Resp getPageDonate(JSONObject jo) {
		
	    if (containsKey(jo, "pgn", "pgs")) {
			pageNumber = jo.getInt("pgn");
			pageSize = jo.getInt("pgs");
			
			String select = "select d.*,u.user_name as operator_name ";
			StringBuffer sql = new StringBuffer(" from  jg_donate d left join t_user u on u.user_id = d.operator_id  where d.del_flag='0' ");
			
			//时间条件
			if(jo.containsKey("donatetime")) {
				String donateTime = jo.getString("donatetime");
				sql.append(" and d.create_time like '"+donateTime+"%'");					
			}	
			//捐赠人条件
			if(jo.containsKey("name")) {
				String name = jo.getString("name");
				 sql.append(" and d.name like '%"+name+"%'");					
			}	
			
			Page<Record> page = Db.paginate(pageNumber, pageSize, select.toString(), sql.toString());
			if(null != page) {
				resp.setCode(CodeConst.SUCCESS);
				resp.setMsg("查询成功");
				resp.setData(page);
			}else {
				resp.setCode(CodeConst.WARN);
				resp.setMsg("查询的数据为空");;
			}
			
	    } else {
			resp.setCode( CodeConst.JSONFORMAT_WRONG);
			resp.setMsg("JSON参数不合法");
		}
		return resp;
	}

	@Before(Tx.class)
	public Resp addDonate(JSONObject jo, Record user) {
		 if (containsKey(jo, "name", "donatetime", "detail")) {
			 Donate donate = new Donate();
			 //捐赠人
			 donate.set("name", jo.get("name"));
			 //捐赠时间
			 donate.set("create_time", jo.get("donatetime"));
			 //捐赠详情
			 donate.set("detail", jo.get("detail"));
			 //操作人
			 donate.set("operator_id", user.get("user_id"));
			 //删除标志
			 donate.set("del_flag", "0");
			 
			//捐赠金额
			if(jo.containsKey("sum")) {
				donate.set("sum", jo.get("sum"));			
			}
			//id
			donate.set("donate_id", UUIDGenerator.getUUID());
			if(donate.save()) {
				resp.setCode(CodeConst.SUCCESS);
				resp.setMsg("捐赠添加成功");
			} else {
				resp.setCode(CodeConst.DONATE_SAVE_FAIL);
				resp.setMsg("捐赠添加失败");
			}
		 } else {
			 resp.setCode( CodeConst.JSONFORMAT_WRONG);
			 resp.setMsg("JSON参数不合法");
		 }
		return resp;
	}
	
	@Before(Tx.class)
	public Resp updateDonate(JSONObject jo, Record user) {
		if (containsKey(jo, "donateid", "name", "donatetime", "detail")) {
			Donate donate = Donate.dao.findById(jo.get("donateid"));
			if(null != donate) {
				//捐赠人
				donate.set("name", jo.get("name"));
				//捐赠时间
				donate.set("update_time", jo.get("donatetime"));
				//捐赠详情
				donate.set("detail", jo.get("detail"));
				//操作人
				donate.set("operator_id", user.get("user_id"));
				//删除标志
				donate.set("del_flag", "0");
				
				//捐赠金额
				if(jo.containsKey("sum")) {
					donate.set("sum", jo.get("sum"));			
				}
				if(donate.update()) {
					resp.setCode(CodeConst.SUCCESS);
					resp.setMsg("捐赠修改成功");
				} else {
					resp.setCode(CodeConst.DONATE_UPDATE_FAIL);
					resp.setMsg("捐赠修改失败");
				}
			} else {
				resp.setCode( CodeConst.DONATE_NOT_FOUND);
				resp.setMsg("修改的捐赠不存在");
			}
		} else {
			resp.setCode( CodeConst.JSONFORMAT_WRONG);
			resp.setMsg("JSON参数不合法");
		}
		return resp;
	}
	
	/**
	 * 删除捐赠
	 * @param jo
	 * @return
	 */
	@Before(Tx.class)
	public Resp deleteDonate(JSONObject jo, Record user) {
		if (containsKey(jo, "donateid")) {
			String donateid = jo.getString("donateid");
			Donate donate = Donate.dao.findById(donateid);
			if(null != donate) {
				//删除
				donate.set("del_flag", "1");
				//操作人
				donate.set("operator_id", user.get("user_id"));
				//操作时间
				donate.set("update_time", DateUtil.getDate(DateUtil.timeFormat));
				if(donate.update()) {
					resp.setCode(CodeConst.SUCCESS);
					resp.setMsg("删除成功");
				}else {
					resp.setCode(CodeConst.DONATE_DELETE_FAIL);
					resp.setMsg("捐赠删除失败");
				}
			}else {
				resp.setCode(CodeConst.DONATE_NOT_FOUND);
				resp.setMsg("删除的捐赠信息不存在");
			}
			
		}else {
			resp.setCode(CodeConst.JSONFORMAT_WRONG);
			resp.setMsg("JSON参数不合法");
		}
		return resp;
	}

	public Resp getOneDonate(JSONObject jo) {
		if (containsKey(jo, "donateid")) {
			Record donate = Donate.dao.findDetailById(jo.getString("donateid"));
			if(null != donate) {
				resp.setCode(CodeConst.SUCCESS);
				resp.setMsg("查询成功");
				resp.setData(donate);
			} else {
				resp.setCode(CodeConst.DONATE_NOT_FOUND);
				resp.setMsg("查询的捐赠信息不存在");
			}
		} else {
			resp.setCode(CodeConst.JSONFORMAT_WRONG);
			resp.setMsg("JSON参数不合法");
		}
		return resp;
	}
	
	
}
